<?php

class Sitemap extends CI_Controller
{
    
    function __construct()
    {
          parent::__construct();
    }

    
    function index()
    {
        //$this->load->model('etiquetas_model');
        //$this->load->model('rubros_model');
        $this->load->model('empresas_model');
        $this->load->library('sitemaps');
        
        
        $empresas=$this->empresas_model->xml_empresas();
        //$etiquetas=$this->etiquetas_model->list_tag_group();
        //$rubros=$this->rubros_model->rubros_count();
        
        
       $item = array(
            "loc" => base_url(),
            "changefreq" => "hourly",
            "priority" => "1"
        );
        $this->sitemaps->set_item_key("loc,changefreq,priority");
        $this->sitemaps->add_item($item);
        $item = array(
            "loc" => base_url()."nosotros.html",
            "changefreq" => "never",
            "priority" => "1"
        );
        $this->sitemaps->set_item_key("loc,changefreq,priority");
        $this->sitemaps->add_item($item);
        $item = array(
            "loc" => base_url()."contacto.html",
            "changefreq" => "never",
            "priority" => "1"
        );
        $this->sitemaps->set_item_key("loc,changefreq,priority");
        $this->sitemaps->add_item($item);
        $item = array(
            "loc" => base_url()."politica-de-privacidad.html",
            "changefreq" => "never",
            "priority" => "1"
        );
        $this->sitemaps->set_item_key("loc,changefreq,priority");
        $this->sitemaps->add_item($item);
         
        foreach($empresas as $var)
        {
            $item = array(
                "loc" => base_url()."san-rafael/" . $var['empresa_url'].".html",
                // ISO 8601 format - date("c") requires PHP5
                "lastmod" => date("c", strtotime($var['fecha_actualizacion'])),
                "changefreq" => "never",
                "priority" => "1"
            );
            $this->sitemaps->set_item_key("loc,lastmod,changefreq,priority");
            $this->sitemaps->add_item($item);
        }
        
       /* foreach($rubros as $var)
        {
            $item = array(
                "loc" => base_url()."san-rafael-rubro/" . $var['nombre']."/",
                "changefreq" => "daily",
                "priority" => "1"
            );
            $this->sitemaps->set_item_key("loc,changefreq,priority");
            $this->sitemaps->add_item($item);
        }
        foreach($etiquetas as $var)
        {
            
            $tag=  str_replace(" ", "%20" , $var['nombre']);
            
            $item = array(
                "loc" => base_url()."san-rafael-tag/" . $tag."/",
                "changefreq" => "daily",
                "priority" => "0.85"
            );
            $this->sitemaps->set_item_key("loc,changefreq,priority");
            $this->sitemaps->add_item($item);
        }*/
        
        // file name may change due to compression
        $file_name = $this->sitemaps->build("sitemap.xml");

        $reponses = $this->sitemaps->ping(site_url($file_name));
        
        redirect("");
    }

}